import { fileURLToPath, URL } from "node:url";

import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
  ],

  resolve: {
    alias: {
      "@": fileURLToPath(new URL("./src", import.meta.url)),
    },
  },

  // 本地服务相关的配置
  server: {
    // port: 8080,

    /**
     * 本地开发通过配置 Proxy 来解决跨域问题
     *    PS:
     *        1. 只在本地开发生效，上线是不行的
     *        2. network 中查看请求是不知道目标地址的
     */
    proxy: {
      // /api/admin/base/open/captcha => http://127.0.0.1:8001/admin/base/open/captcha
      "/api": {
        target: "http://127.0.0.1:8001", // 目标地址
        changeOrigin: true, // 修改目标服务接受到的 host 请求头字段是否为目标本身
        rewrite: (path) => path.replace("/api", ""), // 路径重写
      },

      "/tx": {
        target: "https://apis.map.qq.com",
        changeOrigin: true,
        rewrite: (path) => path.replace("/tx", ""),
      },

      // /admin/admin/base/open/captcha => http://127.0.0.1:8001/admin/admin/base/open/captcha
      // "/admin": {
      //   target: "http://127.0.0.1:8001",
      //   changeOrigin: true,
      //   rewrite: (path) => path.replace("/admin", ""),
      // },
      // "/hello": {
      //   target: "http://127.0.0.1:8001",
      //   changeOrigin: true,
      //   rewrite: (path) => path.replace("/hello", ""),
      // },
    },
  },
});
